package com.loan.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loan.entity.Contract;
import com.loan.entity.Customer;
import com.loan.entity.ResponseResult;
import com.loan.entity.User;
import com.loan.service.ContractService;
import com.loan.service.CustomerService;
import com.loan.service.UserService;
import com.loan.util.Audience;
import com.loan.util.JwtUtil;
import com.loan.vo.CustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: CustomerController
 * @Author: DJY
 * @Date: 2022/1/18 9:59
 * Version 1.0
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @Autowired
    UserService userService;

    @Autowired
    Audience audience;
    /**
     * 添加，只需增加本业务员的id，风险等级level不需要填写
     *
     * @param
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseResult<Void> add(@RequestBody Customer customer,@RequestHeader("token") String jwt) {
        Customer customer1 = new Customer();
       int empId=JwtUtil.parseJwtForId(jwt,audience);
        customer1.setEmpId(empId);
        customer1.setUserId(customer.getUserId());
        customer1.setDel("n");
        customerService.addCustomer(customer1);
        return ResponseResult.SUCCESS;
    }

    /**
     * 条件查询
     * @param customer
     * @return
     */

    @RequestMapping("/condition")
    public ResponseResult<List<Customer>> condition(@RequestBody(required = false) Customer customer/*,@RequestHeader("token") String jwt*/){
//        customer.setEmpId(JwtUtil.parseJwtForId(jwt,audience));
        List<Customer> customers = customerService.listByCondition(customer);
        return new ResponseResult<>(customers);
    }

    @PostMapping ("/update")
    @ResponseBody
    public ResponseResult<Void> update(@RequestBody Customer customer){
        customerService.update(customer);
        return ResponseResult.SUCCESS;
    }

    @PostMapping("/del")
    @ResponseBody
    public ResponseResult<Void> del(@RequestBody Customer customer){
        customerService.del(customer);
        return ResponseResult.SUCCESS;
    }

    @RequestMapping("/page")
    public ResponseResult<PageInfo<Customer>> findByPage(@RequestHeader("token") String jwt,@RequestBody @Validated CustomerVO customerVO) {
        Customer customer=customerVO.getCustomer();
        customer.setEmpId(JwtUtil.parseJwtForId(jwt,audience));
        PageInfo<Customer> customerPage = customerService.findByCondition(customerVO.getPageNum(),customer);
        return new ResponseResult<>(customerPage);
    }

    @GetMapping("/listnoemp/{pageNum}")
    public ResponseResult<PageInfo<User>> findNoEmp(@PathVariable int pageNum){
        PageHelper.startPage(pageNum,2);
        PageInfo<User> users = customerService.listNoEmp(pageNum);
        return new ResponseResult<>(users);
    }

    @GetMapping("/listall/{pageNum}")
    public ResponseResult<PageInfo<Customer>> listAll(@RequestHeader("token") String jwt,@PathVariable int pageNum){
        PageHelper.startPage(pageNum,2);
       int empId =JwtUtil.parseJwtForId(jwt,audience);
        PageInfo<Customer> customers = customerService.listAll(empId,pageNum);
        return new ResponseResult<>(customers);
    }

}

